﻿using NETMQLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ZeroMQ.Server
{
    internal class Program
    {
        static SubMQ subMQ;
        static SubMQ subMQ1;
        static PubMQ pubMQ;
        static ReqMQ reqMQ;
        static RepMQ repMQ;
        static void Main(string[] args)
        {
            try
            {
                TCPRPTest();
                
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error:{ex.Message}");
            }
        }

        private static void TCPRPTest()
        {
            pubMQ = new PubMQ("127.0.0.1", 22220);
            Console.WriteLine("PubMQ Initialize Successful: 127.0.0.1:22220");
            subMQ = new SubMQ("127.0.0.1", 22221);
            subMQ.ReceiveFrameStringEvent += SubMQ_ReceiveFrameStringEvent;
            Console.WriteLine("SubMQ Initialize Successful: 127.0.0.1:22221  (No topic)");
            subMQ1 = new SubMQ("127.0.0.1", 22221, "Client");
            subMQ1.ReceiveFrameStringEvent += SubMQ_ReceiveFrameStringEvent;
            Console.WriteLine("SubMQ Initialize Successful: 127.0.0.1:22221  (Client)");
            reqMQ = new ReqMQ("127.0.0.1", 22222);
            Console.WriteLine("ReqMQ Initialize Successful: 127.0.0.1:22222");
            repMQ = new RepMQ("127.0.0.1", 22223);
            repMQ.ReceiveFrameStringEvent += RepMQ_ReceiveFrameStringEvent;
            Console.WriteLine("RepMQ Initialize Successful: 127.0.0.1:22223");

            while (true)
            {
                pubMQ.SendData("Server NoTopic");
                Console.WriteLine($"{DateTime.Now}: PubMQ Send - (Server NoTopic) ; Topic = null");
                pubMQ.SendData("Server Topic", "Server");
                Console.WriteLine($"{DateTime.Now}: PubMQ Send - (Server Topic) ; Topic = Server");
                string result = reqMQ.SendData("Server Request");
                Console.WriteLine($"{DateTime.Now}: ReqMQ Send - (Server Request); Return = {result}");
                Thread.Sleep(2000);
            }
        }

        private static string RepMQ_ReceiveFrameStringEvent(string arg)
        {
            Console.WriteLine($"{DateTime.Now}: RepMQ Receive - {arg}");
            Console.WriteLine($"{DateTime.Now}: RepMQ Return - {arg}");
            return arg;
        }

        private static void SubMQ_ReceiveFrameStringEvent(string obj)
        {
            Console.WriteLine($"{DateTime.Now}: SubMQ Receive - {obj}");
        }
    }
}
